package com.example.szh.mvp.presenter;

import android.Manifest;
import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;

import com.blankj.utilcode.util.PhoneUtils;
import com.example.szh.App;
import com.example.szh.entity.BaseEntity;
import com.example.szh.entity.TokenEntity;
import com.example.szh.mvp.contract.SplashContract;
import com.example.szh.nextwork.presenter.BasePresenter;
import com.example.szh.utils.SpUtils;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;

public class SplashPresenter extends BasePresenter<SplashContract.ISplashIModel, SplashContract.ISplashIView> {
    @Inject
    public SplashPresenter(SplashContract.ISplashIModel iSplashIModel, SplashContract.ISplashIView iSplashIView) {
        super(iSplashIModel, iSplashIView);
    }

    //请求服务器token方法
    public void requuestToken() {
        Map<String, Object> map = new HashMap<>();
        if (ActivityCompat.checkSelfPermission(App.getInstance().getApplicationContext(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        String imei = PhoneUtils.getIMEI();
        map.put("imie",System.currentTimeMillis());
        m.requestToken(map).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this::onNext);
    }

    @Override
    public void onNext(@NonNull BaseEntity baseEntity) {
        //  请求成功逻辑效验
        TokenEntity entity= (TokenEntity) baseEntity;
        if (entity.status.equals("200"))
            //保存token到本地SP存储
            //通知activity跳转
            SpUtils.saveData("token",entity.getValues().token);
            v.refresh();

    }
}
